<template>
  <div>
    <el-breadcrumb separator="/" style="margin-top: 20px">
      <el-breadcrumb-item :to="{ path: '/manager/order/tackOut' }"
        >首页</el-breadcrumb-item
      >
      <el-breadcrumb-item :to="{ path: '/manager/sender/orderInfo' }"
        >接单信息</el-breadcrumb-item
      >
    </el-breadcrumb>

    <el-dialog v-model="dialogFormVisible" title="配送状态" width="500">
      <el-form label-width="100px">
        <el-form-item label="配送状态">
          <el-select-v2
            v-model="senderState.state"
            :options="options"
            placeholder="请选择"
            size="large"
            style="width: 240px"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="ChargeSendState"> 确认修改 </el-button>
        </div>
      </template>
    </el-dialog>

    <el-dialog width="600px" v-model="menuShowDialog" title="菜单详情">
      <el-table :data="menuList" border height="240px">
        <el-table-column label="菜品名称" prop="menuName"></el-table-column>
        <el-table-column label="价格" prop="menuPrice"></el-table-column>

        <el-table-column label="图片">
          <template #default="scope">
            <el-image
              :src="scope.row.menuPicture"
              fit="cover"
              style="height: 45px; width: 45px; border-radius: 5px"
            />
          </template>
        </el-table-column>

        <el-table-column label="数量" prop="count" />
      </el-table>

      <template #footer>
        <div class="dialog-footer">
          <el-button @click="menuShowDialog = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <el-table :data="tableData" style="width: 100%" height="80vh" border>
      <el-table-column prop="username" label="购买人" width="120" />
      <el-table-column prop="shopName" label="店铺" width="120" />
      <el-table-column label="地址" width="220" prop="address" />
      <el-table-column label="备注" prop="demo"></el-table-column>
      <el-table-column label="下单时间" prop="inputTime" width="170"></el-table-column>
      <el-table-column label="配送状态" width="200">
        <template #default="scope">
          {{ scope.row.state }}
        </template>
      </el-table-column>
      <el-table-column prop="demo" label="备注" />
      <el-table-column label="操作" width="240">
        <template #default="scope">
          <el-button
            @click="chooseState(scope.row)"
            type="primary"
            v-show="scope.row.state == '正在配送'"
            link
            >更新状态</el-button
          >
          <el-button type="warning" link @click="ViewMenuDetail(scope.row)"
            >菜品详情</el-button
          >
          <el-button
            type="danger"
            link
            v-show="scope.row.state == '正在配送'"
            @click="BackOrder(scope.row)"
            >退单</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="page"
      v-model:page-size="pageItem"
      :page-sizes="[20, 40, 100, 200]"
      background="blue"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pageTotal"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script lang="ts" setup>
import axios from "axios";
import { ElMessage } from "element-plus";
import Vue, { ref, reactive } from "vue";

const menuShowDialog = ref(false);
const token = localStorage.getItem("student_auth_token");
const tableData = ref([]);
const page = ref(1);
const pageItem = ref(20);
const pageTotal = ref(0);
const dialogFormVisible = ref(false);
const senderState = ref({
  orderId: "",
  state: 0,
});
const menuList = ref([]);
const options = [
  {
    label: "配送中",
    value: 0,
  },
  {
    label: "已送达",
    value: 1,
  },
];

const form = reactive({
  state: 2,
  orderId: 0,
});

const ViewMenuDetail = (row) => {
  //查看菜单详情
  menuShowDialog.value = true;
  menuList.value = row.menuName;
};

const getOrderInfo = () => {
  axios
    .get("/api/sender/order/mySelf/" + page.value + "/" + pageItem.value, {
      headers: {
        token: token,
      },
    })
    .then((res) => {
      // console.log("code line-33 \n\r😆 res:\n\r", res);
      const msg = res.data.message.list;
      tableData.value = msg;
      pageTotal.value = res.data.message.total;
    })
    .catch((err) => {
      console.log("code line-37 \n\r😁 err:\n\r", err);
    });
};
const chooseState = (row) => {
  if (row.state == "正在配送") senderState.value.state = 0;
  if (row.state == "已送达") senderState.value.state = 1;
  if (row.state == "骑手已退单") senderState.value.state = -1;
  senderState.value.orderId = row.orderId;
  dialogFormVisible.value = true;
};

const handleSizeChange = (val: number) => {
  // console.log(`${val} items per page`);
  pageItem.value = val;
  getOrderInfo();
};
const handleCurrentChange = (val: number) => {
  // console.log(`current page: ${val}`);
  page.value = val;
  getOrderInfo();
};

const ChargeSendState = () => {
  axios
    .get(
      "/api/sender/sender/state/" +
        senderState.value.orderId +
        "/" +
        senderState.value.state,
      {
        headers: {
          token: token,
        },
      }
    )
    .then((res) => {
      // console.log("code line-210 \n\r😂 res:\n\r", res);
      if (res.data.success) {
        ElMessage({
          type: "success",
          message: "状态修改成功",
        });
      } else {
        ElMessage({
          type: "error",
          message: res.data.message,
        });
      }
      getOrderInfo();
      dialogFormVisible.value = false;
    })
    .catch((err) => {
      console.log("code line-214 \n\r😑 err:\n\r", err);
    });
};
const BackOrder = (row) => {
  axios
    .get("/api/sender/sender/state/" + row.orderId + "/" + -1, {
      headers: {
        token: token,
      },
    })
    .then((res) => {
      // console.log("code line-210 \n\r😂 res:\n\r", res);
      if (res.data.success) {
        ElMessage({
          type: "success",
          message: "退单成功",
        });
      } else {
        ElMessage({
          type: "error",
          message: res.data.message,
        });
      }
      getOrderInfo();
      menuShowDialog.value = false;
    })
    .catch((err) => {
      console.log("code line-214 \n\r😑 err:\n\r", err);
    });
};
getOrderInfo();
</script>

<style lang="" scoped></style>
